home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH6 / EMAGA6 / control / server / server.cs < prev   
Encoding:
Text File  |  2006-09-18  |  9.4 KB  |  292 lines

  1. //============================================================================
  2. // control/server/server.cs
  3. //
  4. //  server-side game specific module for 3D2E emaga6 sample game
  5. //  provides client connection management and and player/avatar spawning
  6. //
  7. //  Copyright (c) 2003 by Kenneth C.  Finney.
  8. //============================================================================
  9.  
  10. //============================================================================
  11. // GameConnection Methods
  12. // Extensions to the GameConnection class. Here we add some methods
  13. // to handle player spawning and creation.
  14. //============================================================================
  15.  
  16. function OnServerCreated()
  17. //----------------------------------------------------------------------------
  18. // Once the engine has fired up the server, this function is called
  19. //----------------------------------------------------------------------------
  20. {
  21.    $Game::StartTime = 0;
  22.  
  23.    Exec("./misc/camera.cs");
  24.    Exec("./misc/shapeBase.cs");
  25.    Exec("./misc/item.cs");
  26.    Exec("./players/player.cs"); // Load the player datablocks and methods
  27.    Exec("./players/beast.cs"); // Load the player datablocks and methods
  28.    Exec("./players/ai.cs"); // Load the player datablocks and methods
  29.    Exec("./weapons/weapon.cs");
  30.    Exec("./weapons/crossbow.cs");
  31. }
  32.  
  33. function startGame()
  34. {
  35.    if ($Game::Duration) // Start the game timer
  36.       $Game::Schedule = schedule($Game::Duration * 1000, 0, "onGameDurationEnd" );
  37.    $Game::Running = true;
  38.    schedule( 2000, 0, "CreateBots");
  39. }
  40.  
  41. function onMissionLoaded()
  42. {
  43.    // Called by loadMission() once the mission is finished loading.
  44.    // Nothing special for now, just start up the game play.
  45.    startGame();
  46. }
  47.  
  48. function onMissionEnded()
  49. {
  50.    cancel($Game::Schedule);
  51.    $Game::Running = false;
  52. }
  53.  
  54. function GameConnection::OnClientEnterGame(%this)
  55. //----------------------------------------------------------------------------
  56. // Called when the client has been accepted into the game by the server.
  57. //----------------------------------------------------------------------------
  58. {
  59.    // Create a new camera object.
  60.    %this.camera = new Camera() {
  61.       dataBlock = Observer;
  62.    };
  63.    MissionCleanup.add( %this.camera );
  64.  
  65.    // Create a player object.
  66.    %this.spawnPlayer();
  67. }
  68.  
  69. function GameConnection::SpawnPlayer(%this)
  70. //----------------------------------------------------------------------------
  71. // This is where we place the player spawn decision code.
  72. // It might also call a function that would figure out the spawn
  73. // point transforms by looking up spawn markers.
  74. // Once we know where the player will spawn, then we create the avatar.
  75. //----------------------------------------------------------------------------
  76. {
  77.  
  78.    %this.createPlayer("0 0 201 1 0 0 0");
  79. }
  80.  
  81. function GameConnection::CreatePlayer(%this, %spawnPoint)
  82. //----------------------------------------------------------------------------
  83. // Create the player's avatar object, set it up, and give the player control
  84. // of it.
  85. //----------------------------------------------------------------------------
  86. {
  87.    if (%this.player > 0)//The player should NOT already have an avatar object.
  88.    {                     // if he does, that's a Bad Thing.
  89.       Error( "Attempting to create an angus ghost!" );
  90.    }
  91.    // Create the player object
  92.    %player = new Player() {
  93.       dataBlock = MaleAvatar;   // defined in players/player.cs
  94.       client = %this;           // the avatar will have a pointer to its
  95.    };                           // owner's connection
  96.  
  97.    // Player setup...
  98.    %player.setTransform(%spawnPoint); // where to put it
  99.  
  100.    // Update the camera to start with the player
  101.    %this.camera.setTransform(%player.getEyeTransform());
  102.    %player.setEnergyLevel(100);
  103.  
  104.    // Give the client control of the player
  105.    %this.player = %player;
  106.    %this.setControlObject(%player);
  107. }
  108.  
  109. function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
  110. {
  111.  
  112.    // Switch the client over to the death cam and unhook the player object.
  113.    if (IsObject(%this.camera) && IsObject(%this.player))
  114.    {
  115.       %this.camera.setMode("Death",%this.player);
  116.       %this.setControlObject(%this.camera);
  117.    }
  118.    %this.player = 0;
  119.  
  120.    // Doll out points and display an appropriate message
  121.    if (%damageType $= "Suicide" || %sourceClient == %this)
  122.    {
  123.  
  124.    }
  125.    else
  126.    {
  127.    }
  128. }
  129.  
  130. function serverCmdToggleCamera(%client)
  131. //-----------------------------------------------------------------------------
  132. //
  133. //-----------------------------------------------------------------------------
  134. {
  135.    %co = %client.getControlObject();
  136.    if (%co == %client.player)
  137.    {
  138.       %co = %client.camera;
  139.       %co.mode = toggleCameraFly;
  140.    }
  141.    else
  142.    {
  143.       %co = %client.player;
  144.       %co.mode = observerFly;
  145.    }
  146.    %client.setControlObject(%co);
  147. }
  148.  
  149. function serverCmdDropPlayerAtCamera(%client)
  150. //-----------------------------------------------------------------------------
  151. //
  152. //-----------------------------------------------------------------------------
  153. {
  154.    if ($Server::DevMode || IsObject(EditorGui))
  155.    {
  156.       %client.player.setTransform(%client.camera.getTransform());
  157.       %client.player.setVelocity("0 0 0");
  158.       %client.setControlObject(%client.player);
  159.    }
  160. }
  161.  
  162. function serverCmdDropCameraAtPlayer(%client)
  163. //-----------------------------------------------------------------------------
  164. //
  165. //-----------------------------------------------------------------------------
  166. {
  167.    %client.camera.setTransform(%client.player.getEyeTransform());
  168.    %client.camera.setVelocity("0 0 0");
  169.    %client.setControlObject(%client.camera);
  170. }
  171.  
  172.  
  173. function serverCmdUse(%client,%data)
  174. //-----------------------------------------------------------------------------
  175. // Server Item Use
  176. //-----------------------------------------------------------------------------
  177. {
  178.    %client.getControlObject().use(%data);
  179. }
  180.  
  181. function centerPrintAll( %message, %time, %lines )
  182. //-----------------------------------------------------------------------------
  183. //
  184. //-----------------------------------------------------------------------------
  185. {
  186.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  187.       %lines = 1;
  188.  
  189.    %count = ClientGroup.getCount();
  190.    for (%i = 0; %i < %count; %i++)
  191.     {
  192.         %cl = ClientGroup.getObject(%i);
  193.       if( !%cl.isAIControlled() )
  194.          commandToClient( %cl, 'centerPrint', %message, %time, %lines );
  195.    }
  196. }
  197.  
  198. function bottomPrintAll( %message, %time, %lines )
  199. //-----------------------------------------------------------------------------
  200. //
  201. //-----------------------------------------------------------------------------
  202. {
  203.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  204.       %lines = 1;
  205.  
  206.    %count = ClientGroup.getCount();
  207.     for (%i = 0; %i < %count; %i++)
  208.     {
  209.         %cl = ClientGroup.getObject(%i);
  210.       if( !%cl.isAIControlled() )
  211.          commandToClient( %cl, 'bottomPrint', %message, %time, %lines );
  212.    }
  213. }
  214.  
  215. //-------------------------------------------------------------------------------------------------------
  216.  
  217. function centerPrint( %client, %message, %time, %lines )
  218. //-----------------------------------------------------------------------------
  219. //
  220. //-----------------------------------------------------------------------------
  221. {
  222.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  223.       %lines = 1;
  224.  
  225.  
  226.    commandToClient( %client, 'CenterPrint', %message, %time, %lines );
  227. }
  228.  
  229. function bottomPrint( %client, %message, %time, %lines )
  230. //-----------------------------------------------------------------------------
  231. //
  232. //-----------------------------------------------------------------------------
  233. {
  234.    if( %lines $= "" || ((%lines > 3) || (%lines < 1)) )
  235.       %lines = 1;
  236.  
  237.    commandToClient( %client, 'BottomPrint', %message, %time, %lines );
  238. }
  239.  
  240. //-------------------------------------------------------------------------------------------------------
  241.  
  242. function clearCenterPrint( %client )
  243. //-----------------------------------------------------------------------------
  244. //
  245. //-----------------------------------------------------------------------------
  246. {
  247.    commandToClient( %client, 'ClearCenterPrint');
  248. }
  249.  
  250. function clearBottomPrint( %client )
  251. //-----------------------------------------------------------------------------
  252. //
  253. //-----------------------------------------------------------------------------
  254. {
  255.    commandToClient( %client, 'ClearBottomPrint');
  256. }
  257.  
  258. //-------------------------------------------------------------------------------------------------------
  259.  
  260. function clearCenterPrintAll()
  261. //-----------------------------------------------------------------------------
  262. //
  263. //-----------------------------------------------------------------------------
  264. {
  265.     %count = ClientGroup.getCount();
  266.     for (%i = 0; %i < %count; %i++)
  267.     {
  268.         %cl = ClientGroup.getObject(%i);
  269.       if( !%cl.isAIControlled() )
  270.          commandToClient( %cl, 'ClearCenterPrint');
  271.    }
  272. }
  273.  
  274. function clearBottomPrintAll()
  275. //-----------------------------------------------------------------------------
  276. //
  277. //-----------------------------------------------------------------------------
  278. {
  279.     %count = ClientGroup.getCount();
  280.     for (%i = 0; %i < %count; %i++)
  281.     {
  282.         %cl = ClientGroup.getObject(%i);
  283.       if( !%cl.isAIControlled() )
  284.          commandToClient( %cl, 'ClearBottomPrint');
  285.    }
  286. }
  287.  
  288.  
  289. function onNeedRelight() // stub routine to stop console error spam
  290. {
  291. }
  292.